package com.zhengtoudai.ui.fragment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.BaseFragment;
import com.zhengtoudai.injector.component.DaggerActivityComponent;
import com.zhengtoudai.injector.module.ActivityModule;
import com.zhengtoudai.ui.adapter.MyRetiredAdapter;
import com.zhengtoudai.ui.bean.OnlyFinish;
import com.zhengtoudai.ui.presenter.OnlyFinishPresenterImpl;
import com.zhengtoudai.ui.view.MyInvestView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.zhengtoudai.view.swipetoloadlayout.base.OnRefreshListener;
import com.zhengtoudai.view.swipetoloadlayout.base.SwipeToLoadLayout;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的投资--已赎回
 */
public class MyRetiredFragment extends BaseFragment implements OnRefreshListener,OnLoadMoreListener ,MyInvestView<OnlyFinish>{


    @Bind(R.id.swipe_target)
    RecyclerView mSwipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout mRefresh;
    private MyRetiredAdapter mAdapter;

    @Inject
    OnlyFinishPresenterImpl mPresenter;

    private int numPerPage = 10, pageNum = 1;

    private boolean isPullDown;

    private OnlyFinish result;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.pull_recycle, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {
        DaggerActivityComponent.builder()
                .appComponent(App.mApp.getAppComponent())
                .activityModule(new ActivityModule(getActivity()))
                .build()
                .inject(this);
        mPresenter.attachView(this);
        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
        mSwipeTarget.setLayoutManager(new LinearLayoutManager(activity));
        mAdapter=new MyRetiredAdapter();
        mSwipeTarget.setAdapter(mAdapter);
        if (getUserVisibleHint()){
            mRefresh.post(new Runnable() {
                @Override
                public void run() {
                    if (mRefresh != null) {
                        mRefresh.setRefreshing(true);
                    }
                }
            });
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser&&mRefresh!=null&&result==null){
            mRefresh.post(new Runnable() {
                @Override
                public void run() {
                    if (mRefresh != null) {
                        mRefresh.setRefreshing(true);
                    }
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        mPresenter.detachView();
    }

    @Override
    public void onLoadMore() {
        isPullDown=false;
        if (result!=null&&result.getPageInfo()!=null&&result.getPageInfo().getTotalNum()>pageNum){
            pageNum++;
            mPresenter.getOnlyFinish(numPerPage, pageNum);
        }else{
            AbToastUtil.showToast(mContext,"已无更多数据");
            onComplete(mRefresh);
        }
    }

    @Override
    public void onRefresh() {
        isPullDown=true;
        pageNum=1;
        mPresenter.getOnlyFinish(numPerPage, pageNum);
    }

    @Override
    public void onResult(OnlyFinish result) {
        this.result=result;
        if (result.getList() == null || result.getList().size() == 0) {
            AbToastUtil.showToast(mContext, "暂无数据");
        } else {
            if (isPullDown){
                mAdapter.clearData();
            }
            mAdapter.addData(result.getList());
        }
    }

    @Override
    public void dismissDialog() {
        onComplete(mRefresh);
    }
}
